﻿using System;
using System.Collections.Generic;
using System.Text;

namespace DH.Core.Helper
{
   public static class FormatHelper
    {
        /// <summary>
        /// 格式化文本，防止sql 注入
        /// </summary>
        /// <param name="source">原文字</param>
        /// <param name="res">结果文字</param>
        /// <returns></returns>
        public static string FormatString(this string source,string res)
        {
            System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"<script[\s\S]+</script *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@" href *= *[\s\S]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@" on[\s\S]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex(@"<iframe[\s\S]+</iframe *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex(@"<frameset[\s\S]+</frameset *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex10 = new System.Text.RegularExpressions.Regex(@"select", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex11 = new System.Text.RegularExpressions.Regex(@"update", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex12 = new System.Text.RegularExpressions.Regex(@"delete", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            source = regex1.Replace(source, ""); //过滤<script></script>标记
            source = regex2.Replace(source, ""); //过滤href=javascript: (<A>) 属性
            source = regex3.Replace(source, " _disibledevent="); //过滤其它控件的on...事件
            source = regex4.Replace(source, ""); //过滤iframe
            source = regex10.Replace(source, "s_elect");
            source = regex11.Replace(source, "u_pudate");
            source = regex12.Replace(source, "d_elete");
            source = source.Replace("'", "’");
            source = source.Replace("&nbsp;", " ");
            return source;
        }

    }
}
